package LeetCode;

import Constructs.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2024/6/9.
//
//https://leetcode.cn/problems/binary-tree-preorder-traversal/description/
public class 二叉树的前序遍历 {
    public static void main(String[] args) {

    }
    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if(root == null)return res;
        Stack<TreeNode> stack  =new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null){
                res.add(root.val);
                stack.push(root);
                root = root.left;
            }
            root = stack.pop().right;
        }
        return res;
    }

}
